package com.itxx.xmlDemo;

import com.itxx.xmlDemo.domain.Student;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class XmlTest {
    public static void main(String[] args) throws DocumentException {
        //获取解析器对象
        SAXReader saxReader = new SAXReader();
        //将xml加载到内存中
        Document document = saxReader.read(new File("day17\\xml\\Test.xml"));
        //获取根标签
        Element rootElement = document.getRootElement();
        //获取根标签中所有子标签
        List<Element> elements = rootElement.elements();

        //创建集合用于存储学生对象
        ArrayList<Student> list = new ArrayList<>();

        //通过增强for获取子标签集合中的每一个uesr
        for (Element element : elements) {
            //获取user的属性对象的值
            //element.attribute("id").getValue();
            String id = element.attributeValue("id");

            //获取user指定名称为name的子标签对象值
            //element.element("name").getText();
            String name = element.elementText("name");

            //获取user指定名称为password的子标签对象值
            //element.element("password").getText();
            String password = element.elementText("password");

            System.out.println(id + " " + name + " " + password);
            //添加学生对象到集合中
            list.add(new Student(id, name, password));
        }
        for (Student student : list) {
            System.out.println(student.toString());

        }
    }
}
